/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.deployment.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class JavaMailApp {
    
    
    public static void enviarChave(String destinatario, String login, String senha) {
        Properties props = new Properties();
        /**
         * Parâmetros de conexão com servidor Hotmail
         */
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.from", destinatario);
        props.put("mail.debug", "true");
        props.put("mail.smtp.port", "465");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.fallback", "false");
        
        

        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication("ronieresk8@gmail.com", "94293212");
                    }
                });

        /**
         * Ativa Debug para sessão
         */
        session.setDebug(true);

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("ronieresk8@gmail.com")); //Remetente

            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(destinatario)); //Destinatário(s)
            message.setSubject("Chave de Acesso ao Sistema Notário");//Assunto
            message.setText("Chave de Acesso \n\n Login - "+login+ "\n Senha - "+senha);
            
//             FileDataSource fds = new FileDataSource("/home/roniere/NetBeansProjects/Cartorio/Cartorio-war/src/java/br/com/deployment/util/PedidoSelo.java");          
//            message.setDataHandler(new DataHandler(fds));          
//            message.setFileName(fds.getName());   
            
            /**
             * Método para enviar a mensagem criada
             */
            Transport tr = session.getTransport("smtp");
            tr.connect("smtp.gmail.com", "ronieresk8@gmail.com", "94293212");
            message.saveChanges();
            tr.sendMessage(message, message.getAllRecipients());
            tr.close();
//            Transport.send(message);

            System.out.println("Mensagem Enviada com Sucesso");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
    
    public static void megaSena(){
        List<Integer> ms = new ArrayList<Integer>();
        for (int x = 0; x < 10; x++) {
            for (int i = 0; i < 6; i++) {
                int valor = (int) (Math.random() * 60);
                if(!ms.contains(valor)){
                    System.err.print(valor + " - ");
                    ms.add(valor);
                }else{
                    i = i -1;
                }
            }
            System.err.println();
        }
    }

    public static void main(String[] args) {
//       JavaMailApp.enviarChave("ronieresk8@gmail.com","ABBCDJHE","00123834");

    }
}
